<?php

require_once( "PhpFaces/Context/FacesContext.php" );
require_once( "PhpFaces/Event/Listener.php" );
require_once( "PhpFaces/Lifecycle.php" );

/**
 * This does NOT override EventObject as the java version does;
 * instead it stores source locally on $source.
 */
abstract class PhpFaces_Event_FacesEvent {
	private $phaseId;
	private $source;
	
	public function __construct( PhpFaces_Component_UIComponent $component ) {
		// if $component == null throw new IllegalArgumentException
		$this->source = $component;
		$this->phaseId = PhpFaces_PhaseId::ANY_PHASE;
	}
	
	public abstract function isAppropriateListener( PhpFaces_Event_FacesListener $facesListener );
	
	public abstract function processListener( PhpFaces_Event_FacesListener $facesListener );
	
	public function getComponent() {
		return $this->source;
	}
	
	public function queue() {
		$this->getSource()->queueEvent( $this );
	}
	
	public function getPhasseId() {
		return $this->phaseId;
	}
	
	public function setPhaseId( $phaseId ) {
		$this->phaseId = $phaseId;
	}
}


/**
 * PhaseEvent represents the beginning or ending of processing for 
 * a particular phase of the request processing lifecycle, for the 
 * request encapsulated by the specified FacesContext.
 */
class PhpFaces_Event_PhaseEvent {
	private $phaseId;
	private $facesContext;
	// actually a member of EventObject
	private $source;
	
	public function __construct( PhpFaces_Context_FacesContext $facesContext, $phaseId, PhpFaces_Lifecycle $lifecycle ) {
		$this->facesContext = $facesContext;
		$this->phaseId = $phaseId;
		$this->source = $lifecycle;		
	}
	
	/** Return the FacesContext for the request being processed. */
	public function getFacesContext() {
		return $this->facesContext;	
	}
	
	public function getPhaseId() {
		return $this->phaseId;
	}
	
	// this is actually a member of EventObject
	public function getSource() {
		return $this->source;
	}
}

class PhpFaces_Event_ValueChangeEvent extends PhpFaces_Event_FacesEvent {
	private $oldValue;
	private $newValue;
	public function __constrct( PhpFaces_Component_UIComponent $uiComponent, $oldValue, $newValue ) {
		parent::__construct( $uiComponent );
		$this->oldValue = $oldValue;
		$this->newValue = $newValue;
	}
	
	public function isAppropriateListener( PhpFaces_Event_FacesListener $listener ) {
		return $listener instanceof PhpFaces_Event_ValueChangeListener;
	}
	
	public function processListener( PhpFaces_Event_FacesListener $listener ) {
		$listener->processValueChange( $this );
	}
	
	public function getOldValue() {
		return $this->oldValue;
	}
	
	public function getNewValue() {
		return $this->newValue;
	}
}

?>